package com.zhong.gang.app.ui.audit;

import android.graphics.Rect;
import android.view.TouchDelegate;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhong.gang.app.R;
import com.zhong.gang.applib.utils.TimeUtils;

/**
 * @author Yan
 * @date 2020/6/9  15:25
 * @describe 填写此类描述
 * <p>
 * ====================================================
 */
public class AuditAdapter extends BaseQuickAdapter<AuditBean, BaseViewHolder> {
    public AuditAdapter() {
        super(R.layout.item_audit);
    }

    @Override
    protected void convert(BaseViewHolder helper, AuditBean item) {
        if (item.isCheck()) {
            helper.setImageResource(R.id.item_audit_type_img, R.mipmap.icon_audit_ok);
        } else {
            helper.setImageResource(R.id.item_audit_type_img, R.mipmap.icon_audit_no);
        }


        helper.setText(R.id.item_audit_sn, item.getSn())
              .setText(R.id.item_audit_consignor,
                      String.format("货        主:  %s  %s", item.getConsignorName(),
                              item.getConsignorPhone()))
              .setText(R.id.item_audit_account,
                      String.format("付款账号:  %s  %s", item.getConsignorPayName(),
                              item.getConsignorPayBankCard()))
              .setText(R.id.item_audit_time,
                      String.format("提交时间:  %s", TimeUtils.getTime3(item.getCreateTimestamp())))
              .setText(R.id.item_audit_money, String.format("付款金额:  %s元", item.getPayAmount()))
              .setText(R.id.item_audit_ton, String.format("付款吨位:  %s吨", item.getWeight()))
              .setText(R.id.item_audit_limit,
                      String.format("是否限价:  %s", item.isBeyondLimitPrice() ? "是" : "否"))
              .addOnClickListener(R.id.item_audit_type_img);
        setTouchDelegate(helper.getView(R.id.item_audit_type_img), 30);
        int type = item.getType();
        switch (type) {
            case 1:
                helper.setText(R.id.item_audit_order_type, String.format("订单类型:  %s", "外调货业"));
                break;
            case 2:
                helper.setText(R.id.item_audit_order_type, String.format("订单类型:  %s", "驻场接单"));
                break;
            case 4:
                helper.setText(R.id.item_audit_order_type, String.format("订单类型:  %s", "料场收货"));
                break;
            case 8:
                helper.setText(R.id.item_audit_order_type, String.format("订单类型:  %s", "派车单"));
                break;
            default:
                break;
        }
        // 审核状态:0未审核,1审核中,2审核通过,3审核未通过
        int auditStatus = item.getAuditStatus();

        switch (auditStatus) {
            case 1:
                helper.setText(R.id.item_audit_type_text, "审核中")
                      .setBackgroundRes(R.id.item_audit_type_text, R.drawable.shape_audit_two);
                break;
            case 2:
                helper.setText(R.id.item_audit_type_text, "审核通过")
                      .setBackgroundRes(R.id.item_audit_type_text, R.drawable.shape_audit_one);
                break;
            case 3:
                helper.setText(R.id.item_audit_type_text, "审核未通过")
                      .setBackgroundRes(R.id.item_audit_type_text, R.drawable.shape_audit_two);
                break;
            default:
                helper.setText(R.id.item_audit_type_text, "未审核")
                      .setBackgroundRes(R.id.item_audit_type_text, R.drawable.shape_audit_three);
                break;
        }


    }


    /**
     * 扩大view的点击区域
     *
     * @param view             view目标
     * @param expandTouchWidth 扩大区域
     */
    public static void setTouchDelegate(final View view, final int expandTouchWidth) {
        final View parentView = (View) view.getParent();
        parentView.post(new Runnable() {
            @Override
            public void run() {
                final Rect rect = new Rect();
                view.getHitRect(rect);
                // view构建完成后才能获取，所以放在post中执行
                // 4个方向增加矩形区域
                rect.top -= expandTouchWidth;
                rect.bottom += expandTouchWidth;
                rect.left -= expandTouchWidth;
                rect.right += expandTouchWidth;

                parentView.setTouchDelegate(new TouchDelegate(rect, view));
            }
        });
    }
}
